package com.maidou.qiantai.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.google.gson.JsonObject;
import com.maidou.qiantai.dto.order.OrderAddDto;
import com.maidou.qiantai.vo.order.OrderStatusVo;
import com.maidou.qiantai.vo.order.OsOrderItemInfoVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.OsOrder;
import com.maidou.qiantai.service.IOsOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author maidou
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/qiantai/order")
public class OsOrderController extends BaseController
{
    @Autowired
    private IOsOrderService osOrderService;


    /**
     * 新增订单
     */
    @PostMapping("/add-order")
    public AjaxResult addOrder(@RequestBody HashMap<String,Object> map) throws ParseException {
        String cartIds = JSON.toJSONString(map.get("cartIds"));
        List<Long> longs = JSONObject.parseObject(cartIds, new TypeReference<List<Long>>() {
        });
        String addressId = JSON.toJSONString(map.get("addressId"));
        Long aLong = JSONObject.parseObject(addressId, Long.class);
        OrderAddDto orderAddDto = new OrderAddDto();
        orderAddDto.setAddressId(aLong);
        orderAddDto.setCartIds(longs);
        return AjaxResult.success(osOrderService.insertOsOrderAdd(orderAddDto));
    }
//    获取订单项信息详细信息
    /**
     * 查询订单详情
     */
    @GetMapping("/item-info/{id}")
    public AjaxResult orderItemInfo(@PathVariable("id") Long id)
    {
        OrderStatusVo orderStatusVo = osOrderService.selectOsOrderItemInfo(id);
        return AjaxResult.success(orderStatusVo);
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/list-order/{payStatus}")
    public TableDataInfo list(@PathVariable("payStatus") Integer payStatus)
    {
        startPage();
        List<OrderStatusVo> list = osOrderService.selectOsOrderVoList(payStatus);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OsOrder osOrder)
    {
        List<OsOrder> list = osOrderService.selectOsOrderList(osOrder);
        ExcelUtil<OsOrder> util = new ExcelUtil<OsOrder>(OsOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(osOrderService.selectOsOrderById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('qiantai:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OsOrder osOrder)
    {
        return toAjax(osOrderService.insertOsOrder(osOrder));
    }

    /**
     * 修改订单
     */
    @GetMapping("/update-order/{id}")
    public AjaxResult edit(@PathVariable("id") Long id)
    {
        return toAjax(osOrderService.updateOsOrderByOrderId(id));
    }

    /**
     * 删除订单
     */
	@GetMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(osOrderService.deleteOsOrderById(id));
    }
}
